//
//  AppDelegate.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/3/29.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "AppDelegate.h"
#import "RootTabBarViewController.h"
#import "DataBaseMananger.h"
#import <RongIMKit/RongIMKit.h>
#define kRongYunAppKey @"x4vkb1qpvdv9k"

@interface AppDelegate ()<RCIMUserInfoDataSource, RCIMGroupInfoDataSource>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    RootTabBarViewController *rootVC = [[RootTabBarViewController alloc] init];
    self.window.rootViewController = rootVC;
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
    // 融云的各项配置
    [self configOfRongCloud];
    
    // 融云登陆
    [self loginRongCloud];
    
    return YES;
}

#pragma mark - 融云的各项配置
- (void)configOfRongCloud
{
    [[RCIM sharedRCIM] initWithAppKey:kRongYunAppKey];
    
    // 添加消息接收的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveRongCloudMessage:) name:RCKitDispatchMessageNotification object:nil];
    
    // 设置用户信息提供者
    [[RCIM sharedRCIM] setUserInfoDataSource:self];
    // 设置群组信息提供之
    [[RCIM sharedRCIM] setGroupInfoDataSource:self];
}

- (void)didReceiveRongCloudMessage:(NSNotification *)notification
{
    NSLog(@"消息累加了一条");
}

// 融云登陆
- (void)loginRongCloud
{
    // 测试账号
    // userID = 10000, name = 张国文 token = luHWENGa3v/tza4av3u/TPux5y6UqQlClsLzi81CC9HLpLacPAv8kat/sPBeGQxjEEpF+B12aSSTPX7Rmy7jlg==   image = @"http://image.baidu.com/search/detail?ct=503316480&z=0&ipn=false&word=头像&pn=1&spn=0&di=41054441150&pi=0&rn=1&tn=baiduimagedetail&istype=&ie=utf-8&oe=utf-8&in=3354&cl=2&lm=-1&st=&cs=3212931180%2C1043861377&os=1832299155%2C1844350917&simid=0%2C0&adpicid=0&ln=1000&fr=&fmq=1378374347070_R&fm=&ic=0&s=0&se=&sme=&tab=&face=&ist=&jit=&statnum=head&cg=head&bdtype=11&oriquery=头像&objurl=http%3A%2F%2Fimg4.duitang.com%2Fuploads%2Fitem%2F201602%2F23%2F20160223104150_x2jAC.jpeg&fromurl=http%3A%2F%2Fwww.duitang.com%2Fblog%2F%3Fid%3D537579899&gsm=1e"
    // userID = 10001, name = 梅江林, token = 2BuZG+QdjuJ/atly0T3xMfux5y6UqQlClsLzi81CC9HLpLacPAv8kUM137BcE3KJEEpF+B12aST53qHN++NtTg==                          image = @"http://image.baidu.com/search/detail?ct=503316480&z=0&ipn=false&word=头像&pn=2&spn=0&di=103498846000&pi=0&rn=1&tn=baiduimagedetail&istype=&ie=utf-8&oe=utf-8&in=3354&cl=2&lm=-1&st=&cs=769045664%2C2624144147&os=4207854482%2C1717598210&simid=0%2C0&adpicid=0&ln=1000&fr=&fmq=1378374347070_R&fm=&ic=0&s=0&se=&sme=&tab=&face=&ist=&jit=&statnum=head&cg=head&bdtype=0&oriquery=头像&objurl=http%3A%2F%2Ff.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2F241f95cad1c8a7862495b8776109c93d70cf5008.jpg&fromurl=http%3A%2F%2Fzhidao.baidu.com%2Fquestion%2F1605784475635888827.html&gsm=1e"
    // userID = 10002, name = 殷雄, token = f19nF5gEO9QEgxAAppWjx8ULRUNZ/Sbvb51WO14AtJms/1nxhiL4qY0FEh4ymzH4SgWj0M5OOcvt1dqbwMDcsA==              image = @"http://image.baidu.com/search/detail?ct=503316480&z=0&ipn=false&word=头像&pn=3&spn=0&di=95658986500&pi=0&rn=1&tn=baiduimagedetail&istype=&ie=utf-8&oe=utf-8&in=3354&cl=2&lm=-1&st=&cs=3399543288%2C3713437897&os=3622960556%2C3787506715&simid=0%2C0&adpicid=0&ln=1000&fr=&fmq=1378374347070_R&fm=&ic=0&s=0&se=&sme=&tab=&face=&ist=&jit=&statnum=head&cg=head&bdtype=0&oriquery=头像&objurl=http%3A%2F%2Fh.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2F3812b31bb051f81991b9d8dbdcb44aed2f73e787.jpg&fromurl=http%3A%2F%2Fzhidao.baidu.com%2Fquestion%2F2012448654017118188.html&gsm=1e"
    
    // userID = 10003, name = 吕峰, token = yJA7vrIiHDfeCpX/5mPjT/ux5y6UqQlClsLzi81CC9HLpLacPAv8kbCMU99Ujxr+EEpF+B12aSQfVGklgt/l/A==           image = @"http://image.baidu.com/search/detail?ct=503316480&z=0&ipn=false&word=头像&pn=4&spn=0&di=22372741960&pi=0&rn=1&tn=baiduimagedetail&istype=&ie=utf-8&oe=utf-8&in=3354&cl=2&lm=-1&st=&cs=3011550078%2C4051180033&os=1852096082%2C1827446616&simid=0%2C0&adpicid=0&ln=1000&fr=&fmq=1378374347070_R&fm=&ic=0&s=0&se=&sme=&tab=&face=&ist=&jit=&statnum=head&cg=head&bdtype=11&oriquery=头像&objurl=http%3A%2F%2Fimg5.duitang.com%2Fuploads%2Fitem%2F201602%2F16%2F20160216161607_PCmYJ.jpeg&fromurl=http%3A%2F%2Fwww.duitang.com%2Fblog%2F%3Fid%3D537297880&gsm=1e"
    
    // userID = 10004, name = 欧兆, token = wxmUrA8M8vh0BmPOYbkHnefxZuXeSa4OtnrxDqfzzOUS2T9Hn4NWV1wycd1SC0HslMOmRmT7l+jwhHdsB6WjrA==                 image = @"http://image.baidu.com/search/detail?ct=503316480&z=0&ipn=false&word=头像&pn=5&spn=0&di=87335070600&pi=0&rn=1&tn=baiduimagedetail&istype=&ie=utf-8&oe=utf-8&in=3354&cl=2&lm=-1&st=&cs=1605365725%2C1068839864&os=2465295134%2C2183771314&simid=0%2C0&adpicid=0&ln=1000&fr=&fmq=1378374347070_R&fm=&ic=0&s=0&se=&sme=&tab=&face=&ist=&jit=&statnum=head&cg=head&bdtype=11&oriquery=头像&objurl=http%3A%2F%2Fimg4.duitang.com%2Fuploads%2Fitem%2F201511%2F07%2F20151107174431_emPdc.thumb.700_0.jpeg&fromurl=http%3A%2F%2Fwww.duitang.com%2Fblog%2F%3Fid%3D541121749&gsm=1e"
    
    // userID = 10005, name = 沈佳, token = 6JdqYGFiuDsjehurXbzddfux5y6UqQlClsLzi81CC9HLpLacPAv8kVP3dxnxrOX3a56/LGhVc0zYwHnXyjfjhw==                   image = http://image.baidu.com/search/detail?ct=503316480&z=0&ipn=false&word=头像&pn=6&spn=0&di=20370188630&pi=0&rn=1&tn=baiduimagedetail&istype=&ie=utf-8&oe=utf-8&in=3354&cl=2&lm=-1&st=&cs=1097690798%2C834091630&os=4112349828%2C1834772413&simid=3387112219%2C278402686&adpicid=0&ln=1000&fr=&fmq=1378374347070_R&fm=&ic=0&s=0&se=&sme=&tab=&face=&ist=&jit=&statnum=head&cg=head&bdtype=0&oriquery=头像&objurl=http%3A%2F%2Fh.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2Fc8177f3e6709c93d0f5f00e79b3df8dcd1005474.jpg&fromurl=http%3A%2F%2Fzhidao.baidu.com%2Fquestion%2F1925326335561597067.html&gsm=1e
    
    // 登陆融云服务器
    [[RCIMClient sharedRCIMClient] connectWithToken:@"2BuZG+QdjuJ/atly0T3xMfux5y6UqQlClsLzi81CC9HLpLacPAv8kUM137BcE3KJEEpF+B12aST53qHN++NtTg==" success:^(NSString *userId) {
        NSLog(@"当前登录的用户为%@", userId);
        // 保存当前登陆融云的用户ID
        [[NetStatusHandle shareHandle] saveCurrentLoginRongCloudUserID:userId];

    } error:^(RCConnectErrorCode status) {
        NSLog(@"登陆的错误码为:%ld", status);

    } tokenIncorrect:^{
        NSLog(@"token错误");
    }];
    
    

    
    
    
    
    
}


- (void)getUserInfoWithUserId:(NSString *)userId completion:(void (^)(RCUserInfo *))completion
{
    NSLog(@"用户提供者的ID：%@", userId);
    RCUserInfo *user = [[RCUserInfo alloc] init];
    if ([userId isEqualToString:@"10000"]) {
        user.name = @"张国文";
        user.userId = @"10000";
        user.portraitUri = @"http://image.baidu.com/search/detail?ct=503316480&z=0&ipn=false&word=头像&pn=1&spn=0&di=41054441150&pi=0&rn=1&tn=baiduimagedetail&istype=&ie=utf-8&oe=utf-8&in=3354&cl=2&lm=-1&st=&cs=3212931180%2C1043861377&os=1832299155%2C1844350917&simid=0%2C0&adpicid=0&ln=1000&fr=&fmq=1378374347070_R&fm=&ic=0&s=0&se=&sme=&tab=&face=&ist=&jit=&statnum=head&cg=head&bdtype=11&oriquery=头像&objurl=http%3A%2F%2Fimg4.duitang.com%2Fuploads%2Fitem%2F201602%2F23%2F20160223104150_x2jAC.jpeg&fromurl=http%3A%2F%2Fwww.duitang.com%2Fblog%2F%3Fid%3D537579899&gsm=1e";
        return completion(user);
    } else if ([userId isEqualToString:@"10001"]) {
        user.name = @"梅江林";
        user.userId = @"10001";
        user.portraitUri = @"http://s11.sinaimg.cn/bmiddle/4ae319d7h644722e2783a";
        return completion(user);
    } else if ([userId isEqualToString:@"10002"]) {
        user.name = @"殷雄";
        user.userId = @"10002";
        user.portraitUri = @"http://image.baidu.com/search/detail?ct=503316480&z=0&ipn=false&word=头像&pn=3&spn=0&di=95658986500&pi=0&rn=1&tn=baiduimagedetail&istype=&ie=utf-8&oe=utf-8&in=3354&cl=2&lm=-1&st=&cs=3399543288%2C3713437897&os=3622960556%2C3787506715&simid=0%2C0&adpicid=0&ln=1000&fr=&fmq=1378374347070_R&fm=&ic=0&s=0&se=&sme=&tab=&face=&ist=&jit=&statnum=head&cg=head&bdtype=0&oriquery=头像&objurl=http%3A%2F%2Fh.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2F3812b31bb051f81991b9d8dbdcb44aed2f73e787.jpg&fromurl=http%3A%2F%2Fzhidao.baidu.com%2Fquestion%2F2012448654017118188.html&gsm=1e";
        return completion(user);
    } else if ([userId isEqualToString:@"10003"]) {
        user.name = @"吕峰";
        user.userId = @"10003";
        user.portraitUri = @"http://image.baidu.com/search/detail?ct=503316480&z=0&ipn=false&word=头像&pn=4&spn=0&di=22372741960&pi=0&rn=1&tn=baiduimagedetail&istype=&ie=utf-8&oe=utf-8&in=3354&cl=2&lm=-1&st=&cs=3011550078%2C4051180033&os=1852096082%2C1827446616&simid=0%2C0&adpicid=0&ln=1000&fr=&fmq=1378374347070_R&fm=&ic=0&s=0&se=&sme=&tab=&face=&ist=&jit=&statnum=head&cg=head&bdtype=11&oriquery=头像&objurl=http%3A%2F%2Fimg5.duitang.com%2Fuploads%2Fitem%2F201602%2F16%2F20160216161607_PCmYJ.jpeg&fromurl=http%3A%2F%2Fwww.duitang.com%2Fblog%2F%3Fid%3D537297880&gsm=1e";
        return completion(user);
    } else if ([userId isEqualToString:@"10004"]) {
        user.name = @"欧兆";
        user.userId = @"10004";
        user.portraitUri = @"http://image.baidu.com/search/detail?ct=503316480&z=0&ipn=false&word=头像&pn=5&spn=0&di=87335070600&pi=0&rn=1&tn=baiduimagedetail&istype=&ie=utf-8&oe=utf-8&in=3354&cl=2&lm=-1&st=&cs=1605365725%2C1068839864&os=2465295134%2C2183771314&simid=0%2C0&adpicid=0&ln=1000&fr=&fmq=1378374347070_R&fm=&ic=0&s=0&se=&sme=&tab=&face=&ist=&jit=&statnum=head&cg=head&bdtype=11&oriquery=头像&objurl=http%3A%2F%2Fimg4.duitang.com%2Fuploads%2Fitem%2F201511%2F07%2F20151107174431_emPdc.thumb.700_0.jpeg&fromurl=http%3A%2F%2Fwww.duitang.com%2Fblog%2F%3Fid%3D541121749&gsm=1e";
        return completion(user);
    } else if ([userId isEqualToString:@"10005"]) {
        user.name = @"沈佳";
        user.userId = @"10005";
        user.portraitUri = @"http://image.baidu.com/search/detail?ct=503316480&z=0&ipn=false&word=头像&pn=6&spn=0&di=20370188630&pi=0&rn=1&tn=baiduimagedetail&istype=&ie=utf-8&oe=utf-8&in=3354&cl=2&lm=-1&st=&cs=1097690798%2C834091630&os=4112349828%2C1834772413&simid=3387112219%2C278402686&adpicid=0&ln=1000&fr=&fmq=1378374347070_R&fm=&ic=0&s=0&se=&sme=&tab=&face=&ist=&jit=&statnum=head&cg=head&bdtype=0&oriquery=头像&objurl=http%3A%2F%2Fh.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2Fc8177f3e6709c93d0f5f00e79b3df8dcd1005474.jpg&fromurl=http%3A%2F%2Fzhidao.baidu.com%2Fquestion%2F1925326335561597067.html&gsm=1e";
        return completion(user);
    }
    return completion(nil);
    
}

#pragma mark - 群组信息提供者
- (void)getGroupInfoWithGroupId:(NSString *)groupId completion:(void (^)(RCGroup *))completion
{
    if ([groupId isEqualToString:@"1234567"]) {
        RCGroup *group = [[RCGroup alloc] init];
        group.groupId = @"1234567";
        group.groupName = @"一群";
        group.portraitUri = @"";
        return completion(group);
    } else if ([groupId isEqualToString:@"12345678"]) {
        RCGroup *group = [[RCGroup alloc] init];
        group.groupId = @"12345678";
        group.groupName = @"二群";
        group.portraitUri = @"";
        return completion(group);
    }
    return completion(nil);
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    // 在这里进行本地数据的清空
//    NSArray *dataArray = [[DataBaseMananger shareManeger] getDataFromDB];
//    for (int i = 0; i < dataArray.count; i++) {
//        YTKKeyValueItem *item = dataArray[i];
//        NSString *key = item.itemId;
//        [[DataBaseMananger shareManeger] deleteDataWithID:key];
//    }
//    NSLog(@"清空后 = %@", [[DataBaseMananger shareManeger] getDataFromDB]);
  //  [[SDImageCache sharedImageCache] clearDisk];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
